import { RuntimeError } from '@ibiz-template/core';
import { getControlProvider, IControlProvider } from '@ibiz-template/runtime';
import { IControl } from '@ibiz/model-core';
import { FormMDCtrlContentTypeController } from '../form-mdctrl-content-type.controller';

/**
 *  表单多数据部件 部件类型基类控制器
 *
 * @author zk
 * @date 2023-07-28 08:07:04
 * @export
 * @class FormMDCtrlControlController
 */
export class FormMDCtrlControlController extends FormMDCtrlContentTypeController {
  /**
   * 部件的适配器
   *
   * @author lxm
   * @date 2022-08-24 20:08:07
   * @type {{ [key: string]: IControlProvider }}
   */
  provider!: IControlProvider;

  /**
   * 嵌入部件模型
   *
   * @author zk
   * @date 2023-07-28 09:07:12
   * @type {IControl}
   * @memberof FormMDCtrlControlController
   */
  controlModel!: IControl;

  /**
   * 初始化
   *
   * @author zk
   * @date 2023-07-25 10:07:11
   * @protected
   * @return {*}  {Promise<void>}
   * @memberof FormMDCtrlController
   */
  public async onInit(): Promise<void> {
    const model = this.parent.model;
    const contentControl = model.contentControl;
    if (!contentControl) {
      throw new RuntimeError('表单多数据部件未配置嵌入部件');
    }
    this.controlModel = contentControl;
    const controlProvider = await getControlProvider(this.controlModel);
    if (controlProvider) {
      this.provider = controlProvider;
    }
  }
}
